﻿@{
    Layout = "~/Views/Shared/_indexBase1.cshtml";
    <link href="~/SiteUI/System/Option.css" rel="stylesheet" />
}

<div class="Col">
    <div class="OptionPanel">
        <div class="ToolsBar">
            <h4>任务类型</h4>
            <div class="ToolsItem" action="edit" optiontype="TaskType">编辑</div>
            <div class="ToolsItem" action="add" optiontype="TaskType">新建</div>
        </div>
        <div id="lstTaskTypes">

        </div>
    </div>

    <div class="OptionPanel">
        <div class="ToolsBar">
            <h4>业务选项设置</h4>
            <div class="ToolsItem">编辑</div>
            <div class="ToolsItem">新建</div>
        </div>
        <div id="2">

        </div>
    </div>
</div>

<div class="Col">
    <div class="OptionPanel">
        <div class="ToolsBar">
            <h4>任务关联子系统</h4>
            <div class="ToolsItem" action="edit" optionType="TaskRelatedSubSystem">编辑</div>
            <div class="ToolsItem" action="add" optiontype="TaskRelatedSubSystem">新建</div>
        </div>
        <div id="lstSubSystems">

        </div>
    </div>
</div>

<div class="Col">
    <div class="OptionPanel">
        <div class="ToolsBar">
            <h4>业务选项设置</h4>
            <div class="ToolsItem">编辑</div>
            <div class="ToolsItem">新建</div>
        </div>
        <div id="1">

        </div>
    </div>
</div>

<script src="~/SiteUI/BaseScripts/site.js"></script>
<script>
    Env.Lock = {
        'TaskType': false,
        'TaskRelatedSubSystem':false
    };
    function makeOptionItem(_data) {
        return $("<div>").addClass('optionItem').text(_data.Value);
    };

    function refreshOptionList(_opType) {
        switch (_opType.toString()) {
            case 'TaskType':
                new AjaxHelper('get', '/System/GetOptions', {OptionType:'TaskType'}, refreshEnd, "TaskType");
                break;
            case "TaskRelatedSubSystem":
                new AjaxHelper('get', '/System/GetOptions', { OptionType: 'TaskRelatedSubSystem' }, refreshEnd, "TaskRelatedSubSystem");
                break;
        }
    };

    function refreshEnd(rtn, state) {
        if (rtn.IsPositive) {
            switch (state) {
                case 'TaskType':
                    Env.lstTaskTypes.Clear();
                    Env.lstTaskTypes.Append(rtn.Data.Options);
                    break;
                case 'TaskRelatedSubSystem':
                    Env.lstTaskSubSys.Clear();
                    Env.lstTaskSubSys.Append(rtn.Data.Options);
                    break;
            }
        }
    };

    function onToolsClick() {
        var $btn = $(this);
        var action = $btn.attr('action');
        var type = $btn.attr('optionType');
        if (action && type) {
            switch (action) {
                case 'add':
                    addBegin(type);
                    break;
                case 'edit':
                    editBegin(type);
                    break;
            }
        }
    };

    function addBegin(opType) {
        if (Env.Lock[opType] == true) {
            alert('之前的操作还未完成，请稍候重试');
            return;
        }
        Env.Lock[opType] = true;
        switch (opType) {
            case 'TaskType':
                addOption(Env.lstTaskTypes, 'TaskType');
                break;
            case 'TaskRelatedSubSystem':
                addOption(Env.lstTaskSubSys, 'TaskRelatedSubSystem');
                break;
        }
    };

    function editBegin(opType) {
        if (Env.Lock[opType] == true) {
            alert('之前的操作还未完成，请稍候重试');
            return;
        }
        Env.Lock[opType] = true;
        switch (opType) {
            case 'TaskType':
                var n = {};
                var slct = Env.lstTaskTypes.Select();
                if (slct !== undefined) {
                    //如果已经选中
                    editOption(slct);
                }
                else {
                    alert('请选择要编辑的参数项');
                }
                break;
            case 'TaskRelatedSubSystem':
                var slct = Env.lstTaskSubSys.Select();
                if (slct !== undefined) {
                    //如果已经选中，则作为编辑目标
                    editOption(slct);
                }
                else {
                    alert('请选择要编辑的选项');
                }
                break;
        }
    };

    function addOption(lst,type) {
        var no = {};
        no.IDStr = Math.random().toString();
        no.Type = type;
        no.Value = '(' + type + ')';
        var prow = lst.Select();
        if (prow) {
            no.PIdStr = prow.Data.IDStr;
        }
        lst.Append(no);
        var row = lst.Select(lst.Data().length - 1);
        Env.txbOptionValue.dom(row.View).val(row.Data.Value, false).show().select();
        Env.CRow = row;
        Env.Action = 'add';
    };
    function editOption(row) {
        Env.txbOptionValue.dom(row.View).val(row.Data.Value, false).show().select();
        Env.CRow = row;
        Env.Action = 'edit';
    };

    function onInputEnd(ev) {
        if (!Env.CRow) {
            return;
        }
        var nv = ev.target.val();
        Env.txbOptionValue.hide();
        if (ObjHelper.IsEmpty(nv)) {
            return;
        }
        Env.CRow.View.text(nv);
        Env.CRow.Data.Value = nv;

        //调用后台执行
        if (Env.Action == 'edit') {
            new AjaxHelper('get', '/System/UpdateOption', { id: Env.CRow.Data.IDStr, value: nv }, editEnd, Env.CRow);
        }
        else if (Env.Action == 'add') {
            new AjaxHelper('get', '/System/NewOption', { optionType: Env.CRow.Data.Type, value: nv, pid: Env.CRow.Data.PIdStr }, addEnd, Env.CRow);
        }
    };

    function addEnd(rtn, row) {
        Env.Lock[row.Data.Type] = false;
        if (rtn.IsPositive) {
            row.Data.IDStr = rtn.Data.IDStr;
        }
        else {
            refreshOptionList(row.Data.Type);
        }
    };

    function editEnd(rtn, row) {
        Env.Lock[row.Data.Type] = false;
    };

    function onRowDBLClick(ev) {
        this.Select(ev.RowIndex);
        editBegin(ev.Data.Type);
    };

    $(function () {
        Env.lstTaskTypes = new List($("#lstTaskTypes"), { ShowHead: false, ShowFoot: false, Selectble: true });
        Env.lstTaskTypes.af_makeView = makeOptionItem;
        Env.lstTaskTypes.on('itemDBClick', onRowDBLClick);

        Env.lstTaskSubSys = new List($("#lstSubSystems"), { ShowHead: false, ShowFoot: false, Selectble: true });
        Env.lstTaskSubSys.af_makeView = makeOptionItem;
        Env.lstTaskSubSys.on('itemDBClick', onRowDBLClick);

        Env.txbOptionValue = new Textbox().addClass('txbOptionValue');
        Env.txbOptionValue.on('valueChanged', onInputEnd);

        $(".ToolsItem").on('click', onToolsClick);

        refreshOptionList('TaskType');
        refreshOptionList('TaskRelatedSubSystem');
    })
</script>


